package com.javasm.crm.service.impl.productimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.crm.domin.criteria.GoodsSearchCriteria;
import com.javasm.crm.domin.criteria.ProductCriteria;
import com.javasm.crm.domin.entity.productEntity.GoodsSupplier;
import com.javasm.crm.domin.entity.productEntity.ProductInfo;
import com.javasm.crm.domin.entity.productEntity.ProductVo;
import com.javasm.crm.mapper.goodsmapper.GoodsSupplierMapper;
import com.javasm.crm.mapper.goodsmapper.ProductVoMapper;
import com.javasm.crm.service.productservice.GoodsSupplierService;
import com.javasm.crm.service.productservice.ProductVoService;
import com.javasm.crm.utils.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @Author：zhangyang
 * @Version：1.0
 * @Date：2021/7/12-17:00
 * @desp:
 * @since:
 */
@Service
@Transactional
public class GoodsSupplierServiceImpl extends ServiceImpl<GoodsSupplierMapper, GoodsSupplier> implements GoodsSupplierService {
    @Autowired
    GoodsSupplierMapper goodsSupplierMapper;


    @Override
    public PageResult<GoodsSupplier> searchPageByCriteria(GoodsSearchCriteria goodsSearchCriteria) {
        Integer currentPage = goodsSearchCriteria.getCurrentPage();

        Integer pageSize = goodsSearchCriteria.getPageSize();
        if (StringUtils.isEmpty(currentPage)) {
            currentPage=1;
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize=5;
        }
        QueryWrapper<GoodsSupplier> exampleQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(goodsSearchCriteria.getSupplierName())){
            exampleQueryWrapper.like("supplier_name",goodsSearchCriteria.getSupplierName());
        }
        if (!StringUtils.isEmpty(goodsSearchCriteria.getSupplierContactman())) {
            exampleQueryWrapper.like("supplier_contactman", goodsSearchCriteria.getSupplierContactman());
        }
        if (!StringUtils.isEmpty(goodsSearchCriteria.getSupplierPhone())) {
            exampleQueryWrapper.like("supplier_phone", goodsSearchCriteria.getSupplierPhone());
        }
        if (!StringUtils.isEmpty(goodsSearchCriteria.getSupplierAddr())) {
            exampleQueryWrapper.like("supplier_addr", goodsSearchCriteria.getSupplierAddr());
        }
        if (!StringUtils.isEmpty(goodsSearchCriteria.getSupplierAccount())) {
            exampleQueryWrapper.like("supplier_account", goodsSearchCriteria.getSupplierAccount());
        }
        if (!StringUtils.isEmpty(goodsSearchCriteria.getSupplierBank())) {
            exampleQueryWrapper.like("supplier_bank", goodsSearchCriteria.getSupplierBank());
        }
        Page<GoodsSupplier> examplePage = new Page<>(currentPage, pageSize);
        List<GoodsSupplier> queryall = goodsSupplierMapper.queryall(examplePage, exampleQueryWrapper);

        return new PageResult<GoodsSupplier>(examplePage.getTotal(), queryall);
    }

    @Override
    public int add(GoodsSupplier goodsSupplier) {
        return goodsSupplierMapper.insert(goodsSupplier);
    }

    @Override
    public int deleteById(Serializable productId) {
        return goodsSupplierMapper.deleteById(productId);
    }

    @Override
    public int update(GoodsSupplier goodsSupplier) {
        return goodsSupplierMapper.updateById(goodsSupplier);
    }
}
